package com.example.zqm.git72.Activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.zqm.git72.Fragment.ClubFragment;
import com.example.zqm.git72.Fragment.HomeFragment;
import com.example.zqm.git72.Fragment.MyFragment;
import com.example.zqm.git72.Fragment.PhotoFragment;
import com.example.zqm.git72.Fragment.TrainFragment;
import com.example.zqm.git72.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private LinearLayout mTabHome;
    private LinearLayout mTabTrain;
    private LinearLayout mTabClub;
    private LinearLayout mTabPhoto;
    private LinearLayout mTabMy;

    private ImageButton mImgHome;
    private ImageButton mImgTrain;
    private ImageButton mImgClub;
    private ImageButton mImgPhoto;
    private ImageButton mImgMy;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
        setSelect(0);
    }

    private void initEvent() {
        mTabHome.setOnClickListener(this);
        mTabTrain.setOnClickListener(this);
        mTabClub.setOnClickListener(this);
        mTabPhoto.setOnClickListener(this);
        mTabMy.setOnClickListener(this);
    }

    private void initView() {
        mTabHome = (LinearLayout) findViewById(R.id.id_tab_home);
        mTabTrain = (LinearLayout) findViewById(R.id.id_tab_train);
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        mTabPhoto = (LinearLayout) findViewById(R.id.id_tab_photo);
        mTabMy = (LinearLayout) findViewById(R.id.id_tab_my);

        mImgHome = (ImageButton) findViewById(R.id.id_tab_home_img);
        mImgTrain = (ImageButton) findViewById(R.id.id_tab_train_img);
        mImgClub= (ImageButton) findViewById(R.id.id_tab_club_img);
        mImgPhoto= (ImageButton) findViewById(R.id.id_tab_photo_img);
        mImgMy = (ImageButton) findViewById(R.id.id_tab_my_img);
    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {
            case 0:
                if (mTab01 == null) {
                    mTab01 = new HomeFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else {
                    transaction.show(mTab01);
                }
                mImgHome.setImageResource(R.drawable.tab_home_pressed);
                break;
            case 1:
                if (mTab02 == null) {
                    mTab02 = new TrainFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else {
                    transaction.show(mTab02);

                }
                mImgTrain.setImageResource(R.drawable.tab_train_pressed);
                break;
            case 2:
                if (mTab03 == null) {
                    mTab03 = new ClubFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else {
                    transaction.show(mTab03);
                }
                mImgClub.setImageResource(R.drawable.tab_club_pressed);
                break;
            case 3:
                if (mTab04 == null) {
                    mTab04 = new PhotoFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else {
                    transaction.show(mTab04);
                }
                mImgPhoto.setImageResource(R.drawable.tab_photo_pressed);
                break;
            case 4:
                if (mTab05 == null) {
                    mTab05 = new MyFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else {
                    transaction.show(mTab05);
                }
                mImgMy.setImageResource(R.drawable.tab_my_pressed);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01 != null) {
            transaction.hide(mTab01);
        }
        if (mTab02 != null) {
            transaction.hide(mTab02);
        }
        if (mTab03 != null) {
            transaction.hide(mTab03);
        }
        if (mTab04 != null) {
            transaction.hide(mTab04);
        }
        if (mTab05 != null) {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId()) {
            case R.id.id_tab_home:
                setSelect(0);
                break;
            case R.id.id_tab_train:
                setSelect(1);
                break;
            case R.id.id_tab_club:
                setSelect(2);
                break;
            case R.id.id_tab_photo:
                setSelect(3);
                break;
            case R.id.id_tab_my:
                setSelect(4);
                break;

            default:
                break;
        }
    }


    private void resetImgs() {
        mImgHome.setImageResource(R.drawable.tab_home_normal);
        mImgTrain.setImageResource(R.drawable.tab_train_normal);
        mImgClub.setImageResource(R.drawable.tab_club_normal);
        mImgPhoto.setImageResource(R.drawable.tab_photo_normal);
        mImgMy.setImageResource(R.drawable.tab_my_normal);
    }
}
